/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#include "dockwidgetcoorsinfo.h"
#include "ui_dockwidgetcoorsinfo.h"
#include <QGraphicsBlurEffect>
#include "mapvision.h"


DockWidgetCoorsInfo::DockWidgetCoorsInfo(MapVision *parent, QAction *act) :
    DockWidgetGeneral(parent), ui(new Ui::DockWidgetCoorsInfo)
{
    connect(getMapView(),SIGNAL(clickOnLonlat(QPointF)),this,SLOT(clicked(QPointF)));

    bar = new QToolBar(this);
    bar->layout()->setContentsMargins(0,0,0,0);
    bar->layout()->setSpacing(0);
    bar->layout()->setMargin(0);

    ui->setupUi(this);
    ui->horizontalLayout_2->addWidget(bar);

    this->setWindowTitle(tr("Coordinates Info"));

    degrees = new QCheckBox;
    degrees->setText(tr("degrees"));

    action=act;
    this->addAction(action);
    this->addAction(ui->actionUtm);
    this->addAction(ui->actionCopy_to_console);

    bar->addActions(this->actions());
    bar->addWidget(degrees);
    isDMSformat=true;

}

DockWidgetCoorsInfo::~DockWidgetCoorsInfo()
{
    delete ui;
}


void DockWidgetCoorsInfo::clicked(QPointF lonlat)
{
    if (action->isChecked()) {

        if (ui->plainTextEdit->toPlainText().contains("+units=m")){
            degrees->setChecked(false);
        } else {
            degrees->setChecked(true);
        }

        ZavaProj zavaProj;
        if (isDMSformat){
            ui->lineEdit_2->setText(zavaProj.lonlat2dms(lonlat));
        } else {
            ui->lineEdit_2->setText(QString::number(lonlat.y(),'f',4)+","+QString::number(lonlat.x(),'f',4));
        }
        QPointF lonlat3=zavaProj.lonlat2UTM(lonlat);
        lonlat3=zavaProj.project(lonlat,ui->plainTextEdit->toPlainText(),degrees->isChecked());
        ui->lineEdit->setText(QString::number(lonlat3.x(),'f',4)+","+QString::number(lonlat3.y(),'f',4));
    }
}


void DockWidgetCoorsInfo::on_actionCopy_to_console_triggered()
{
    QString units = (degrees->isChecked())?"[°]":"[m]";

    QString message ="<table border=1 bordercolor=\"solid black\" style=\"border-collapse:collapse; background-color:#FFFFCC\" cellpadding=\"1\" cellspacing=\"1\">";
    message+="<tr>"\
            "<th align=\"center\" colspan=2>COORDINATES</th>\n"\
            "</tr><tr align=\"right\">"\
            "<th>WGS84 [°]</th><td>"+ui->lineEdit_2->text()+"</td>\n"\
            "</tr><tr align=\"right\">"\
            "<th>DEFINED "+units+"</th><td>"+ui->lineEdit->text()+"</td>\n"\
            "</tr>"\
            "</table><br/>\n";

    qDebug()<< message;

    mapVision->printMessage(message);
}

void DockWidgetCoorsInfo::on_actionUtm_triggered()
{
    if (isDMSformat){
        isDMSformat= false;
        ui->actionUtm->setText(tr("DMS format"));
    } else {
        isDMSformat= true;
        ui->actionUtm->setText(tr("decimal format"));
    }
}
